package com.nf.health.app.intervention_details.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.fragment.IBaseFragment;
import com.nf.health.app.http.Act;
import com.nf.health.app.intervention_details.bean.InterventionCoverBean;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.ImageLoaderUtils;
import com.nf.health.app.utils.LogUtil;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.Util;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 封面fragment
 * Created by hepan on 2016/11/10.
 */
public class InterventionCoverFragment extends IBaseFragment {

    @Bind(R.id.iv_hospital_icon)
    CircleImageView ivHospitalIcon;
    @Bind(R.id.tv_intervention_name)
    TextView tvInterventionName;
    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_sex)
    TextView tvSex;
    @Bind(R.id.tv_glyy)
    TextView tvGlyy;
    @Bind(R.id.tv_age)
    TextView tvAge;
    @Bind(R.id.tv_zlys_keshi)
    TextView tvZlysKeshi;
    @Bind(R.id.tv_zlys_name)
    TextView tvZlysName;
    @Bind(R.id.tv_glys_keshi)
    TextView tvGlysKeshi;
    @Bind(R.id.tv_glys_name)
    TextView tvGlysName;

    private boolean isVisible = false;
    private boolean isPrepared = false;
    private HashMap<String, InterventionCoverBean> mBeanMap = new HashMap<>();//存储封面bean集合
    private InterventionCoverBean mShowBean;//当前显示的bean类

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addViewFillInRoot(R.layout.fragment_intervention_cover);
        ButterKnife.bind(this, rootView);
        isPrepared = true;
        lazyLoadData();
        initDialog();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            lazyLoadData();
        } else {
            isVisible = false;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        lazyLoadData();
    }

    /**
     * 加载数据
     */
    public void lazyLoadData() {
        if (!isPrepared) {
            return;
        }
        LogUtil.e("hepan" + "封面onResume >>hint" + getUserVisibleHint());
        String id = Constants.CHANGE_ABLE.INTERVENTION_ID;
        //集合中有数据就不在请求
        if (mBeanMap.containsKey(id)) {
            mShowBean = mBeanMap.get(id);
            try {
                initCoverView();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            //请求数据
            provider.getInterveneCover(id);
        }

    }

    /**
     * 显示封面view
     */
    private void initCoverView() {
        //医院头像,名称
//        Glide.with(getActivity()).load(mShowBean.getHimg())
//                .placeholder(R.drawable.default_square_image).into(ivHospitalIcon);
        ImageLoaderUtils.getinstance(getActivity()).getImage(ivHospitalIcon, mShowBean.getHimg(), R.drawable.default_square_image);
        tvInterventionName.setText(mShowBean.getHname());
        //患者基本信息
        tvPatientName.setText(mShowBean.getUsername());
        tvSex.setText("性  别:  " + Util.getSex(mShowBean.getSex()));
        tvAge.setText("年  龄:  " + mShowBean.getAge());
        //医院 医生信息
        tvGlyy.setText("管理医院:  " + mShowBean.getHname());
        tvGlysKeshi.setText(mShowBean.getDepartment());
        tvGlysName.setText(mShowBean.getDoctorname());
        tvZlysKeshi.setText( mShowBean.getMdepartment());
        tvZlysName.setText(mShowBean.getMdoctorname());
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if (Act.Flag.GET_INTERVENE_COVER.equals(actionName)) {
            mShowBean = (InterventionCoverBean) object;
            if (!mBeanMap.containsKey(mShowBean.getId())) {
                mBeanMap.put(mShowBean.getId(), mShowBean);
            }
            //再次调用,保证更新ui的入口唯一
            lazyLoadData();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        isPrepared = false;
    }

    private void initDialog(){
        String guideFlag = PreferenceHelper.getString(PreferenceHelper.COVERYINDAO, "");
        if(TextUtils.isEmpty(guideFlag)){
            final Dialog dialog = new Dialog(getActivity(), R.style.Dialog_Fullscreen);
            LinearLayout ll = new LinearLayout(getContext());
            ll.setGravity(Gravity.CENTER);
            ImageView guideImage = new ImageView(getContext());
            guideImage.setImageResource(R.drawable.bg_cover_guide);
            guideImage.setLayoutParams(new LinearLayout.LayoutParams(DensityUtil.dip2px(getContext(),274),
                    DensityUtil.dip2px(getContext(),73), Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL));

            guideImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    PreferenceHelper.putString(PreferenceHelper.COVERYINDAO, "1");
                }
            });
            ll.addView(guideImage);
            dialog.setContentView(ll);
            dialog.show();
        }
    }
}
